package com.woniuxy.controller;

import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/list")
    @ResponseBody
    public List<User> users(){
        List<User> users = userService.queryAll();
        return users;
    }
    @RequestMapping("/add")
    public Integer addUser(@RequestParam(value = "name")String name,
                           @RequestParam(value = "age",required = false,defaultValue = "30")Integer age,
                           @RequestParam(value = "password")String password){
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setAge(age);
        Integer add = userService.add(user);
        return add;
    }
    @RequestMapping("/byId")
    public User user(@RequestParam(value = "id") Integer id){
        User user = userService.queryById(id);
        return user;
    }
    @RequestMapping("/byCondition")
    public List<User> queryByCondition(@RequestParam(value = "name")String name,
                                       @RequestParam(value = "gender")String gender){
        List<User> users = userService.queryByCondition(name,gender);
        return users;
    }
    @RequestMapping("/update")
    public Integer update(User user){
        Integer update = userService.updateByIdSelective(user);
        return update;
    }
    @RequestMapping("/delete")
    public Integer delete(@RequestParam(value = "id") Integer id){
        Integer delete = userService.delete(id);
        return delete;
    }
    @RequestMapping("/add2")
    public Integer add(@RequestParam(value = "username")String name,
                           @RequestParam(value = "age",required = false,defaultValue = "30")Integer age){
        System.out.println(name);
        age++;
        System.out.println(age);
        return 1;
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Map<String,Object> login(@RequestBody User user){
        Map<String, Object> login = userService.login(user);
        return login;
    }
}
